﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.Core.Calculations;

namespace ITAP.Core.Modelling
{
    [Serializable]
    public class Interval
    {
        private double start = 0;

        public double Start
        {
            get { return start; }
            set 
            {
                if (value > end)
                    start = end;
                else
                    start = value; 
            }
        }
        private double end = 0;

        public double End
        {
            get { return end; }
            set 
            {
                if (value < start)
                    end = start;
                else
                    end = value; 
            }
        }

        public double Center
        {
            get
            {
                return (this.start + this.end) / 2;
            }
        }

        public Interval(double start, double end)
        {
            this.start = start;
            this.end = end;
            this.Recalculate();
        }

        private void Recalculate()
        {
            if (this.end < this.start)
                this.end = this.start;            
        }

        public void Normalize()
        {
            this.start = this.start / this.end;
            this.end = 1;
        }

        public bool IsInInterval(double value)
        {
            if (value >= this.start && value <= this.end)
                return true;
            else
                return false;
        }

        public static bool IsInInterval(double start, double end, double value, double tolerance)
        {
            double max = Math.Max(start, end);
            double min = Math.Min(start, end);
            if ((value < max || SimpleEqualProvider.IsEqual(value, max, tolerance)) && 
                (value > min || SimpleEqualProvider.IsEqual(value, min, tolerance)))
                return true;
            else
                return false;
        }
    }
}
